//
//  UserDownloadViewController.m
//  XinKaiFa55like
//
//  Created by junseek on 2017/3/26.
//  Copyright © 2017年 55like lj. All rights reserved.
//下载专区

#import "UserDownloadViewController.h"
#import "RHTableView.h"
#import "UserDownloadTableViewCell.h"
#import "SelectWebUrlViewController.h"

@interface UserDownloadViewController (){
    UIScrollView *viewTopBG;
    RHTableView *table_user;
    UIView *viewLine;
    NSArray *arrayType;
    NSString *strType_cid;
}

@end

@implementation UserDownloadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initTabelViews];
    [self initTopButtonClicked];
    
}
-(void)initTopButtonClicked{
    viewTopBG=[[UIScrollView alloc] initWithFrame:CGRectMake(0, kTopHeight, kScreenWidth, 40)];
    viewTopBG.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:viewTopBG];
    
    UIView*linview=  [RHMethods lineViewWithFrame:CGRectMake(0, YH(viewTopBG)-0.5, kScreenWidth, 0.5)];
    [self.view addSubview:linview];
    
    viewLine=[RHMethods lineViewWithFrame:CGRectMake(0, H(viewTopBG)-2, 10, 2)];
    [viewLine setBackgroundColor:rgbpublicColor];
    [viewTopBG addSubview:viewLine];
    arrayType=self.otherInfo;
    
    float fjg=10;
    float fx=fjg;
    for (int i=0;i<[arrayType count]; i++) {
        NSString *strTitle=[[arrayType objectAtIndex:i] valueForJSONStrKey:@"name"];
        UIButton *btnT=[RHMethods buttonWithFrame:CGRectMake(fx, 0, 100, H(viewTopBG)) title:strTitle image:nil bgimage:nil];
        [viewTopBG addSubview:btnT];
        btnT.tag=10+i;
        [btnT setTitleColor:rgbpublicColor forState:UIControlStateSelected];
        [btnT addTarget:self action:@selector(typeSelectButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        float fw=[btnT.titleLabel sizeThatFits:CGSizeMake(MAXFLOAT, 20)].width+16;
        btnT.frameWidth=fw;
        fx+=fw+fjg;
        if (i==0) {
            [self typeSelectButtonClicked:btnT];
        }
    }
    [viewTopBG setContentSize:CGSizeMake(fx, 39)];
    if ([[Utility Share] userIsADD]) {
        viewTopBG.hidden = YES;
        viewLine.hidden = YES;
        linview.hidden=YES;
    }
}
-(void)initTabelViews{
    table_user=[[RHTableView alloc]initWithFrame:CGRectMake(0, kTopHeight+40, kScreenWidth, kContentHeight-40)];
    if ([[Utility Share] userIsADD]) {
        viewTopBG.hidden = YES;
        table_user.frameY = 64;
        table_user.frameHeight = kScreenHeight - 64;
        
    }
    
    [table_user showRefresh:YES LoadMore:YES];
    table_user.delegate = self;
    table_user.dataSource = self;
    [table_user setBackgroundColor:rgbGray];
    [table_user setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:table_user];
    [table_user setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    [table_user registerClass:[UserDownloadTableViewCell class] forCellReuseIdentifier:@"UserDownloadTableViewCell"];
}

-(void)loadTableData{
    krequestParam
    [dictparam setValue:@"%@" forKey:@"page"];
    [dictparam setValue:@"20" forKey:@"pagesize"];
    [dictparam setValue:strType_cid forKey:@"cid"];
    table_user.urlString=[NSString stringWithFormat:@"%@%@",XKFV3productsdownlist,dictparam.wgetParamStr];
    [table_user refresh];
}
#pragma mark button
-(void)typeSelectButtonClicked:(UIButton *)btn{
    for (UIView *view in [viewTopBG subviews]) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *btnT=(UIButton *)view;
            btnT.selected=NO;
        }
    }
    strType_cid=[[arrayType objectAtIndex:btn.tag-10] valueForJSONStrKey:@"id"];
    [self loadTableData];
    btn.selected=YES;
    viewLine.frameWidth=btn.frameWidth;
    [UIView animateWithDuration:0.3 animations:^{
        viewLine.frameX=X(btn);
    }];
    
    
}
#pragma mark tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return table_user.dataArray.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic=[table_user.dataArray objectAtIndex:indexPath.row];
    UserDownloadTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"UserDownloadTableViewCell" forIndexPath:indexPath];
    [cell setValueForDictionary:dic indexPath:indexPath type:self.userInfo];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ///web页
    NSDictionary *dic=[table_user.dataArray objectAtIndex:indexPath.row];
    [self pushController:[SelectWebUrlViewController class] withInfo:@"userDownload" withTitle:@" " withOther:dic];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
